#!/usr/bin/env bash

set -e

root_dir=$PWD

if [ "$CI" == true ]; then
  set -x
  tree_sitter="$TREE_SITTER"
else
  cargo build --release
  tree_sitter=${root_dir}/target/release/tree-sitter
fi

filter_grammar_name=$1

grammars_dir=${root_dir}/test/fixtures/grammars
grammar_files=$(find "$grammars_dir" -name grammar.js | grep -v node_modules)

while read -r grammar_file; do
  grammar_dir=$(dirname "$grammar_file")
  grammar_name=$(basename "$grammar_dir")

  if [[ -n $filter_grammar_name && "$filter_grammar_name" != "$grammar_name" ]]; then
    continue
  fi

  echo "Regenerating ${grammar_name} parser"
  (
    cd "$grammar_dir"
    "$tree_sitter" generate src/grammar.json --no-bindings --abi=latest
  )
done <<<"$grammar_files"
